package com.example.lihongwei42.bluetoothuitls;

import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ActivityCompat.OnRequestPermissionsResultCallback {
    private AudioSwitchUtils audioSwitchUtils;
    private Button mPlayMusicBtn;
    private Button mOpenBltBtn;
    private Button mCloseBltBtn;
    private Button mSwitchBltBtn;
    private Button mSwitchSpeakerBtn;
    private Button mSwitchEarphoneBtn;
    private Button mSwitchReceiverBtn;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
//        //申请权限
//        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
//        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
//        }
        audioSwitchUtils = AudioSwitchUtils.getInstance().init(this);
        audioSwitchUtils.setOnHandsetChangeListener(new AudioSwitchUtils.OnHandsetStateChangeListener() {
            @Override
            public void onBtOff() {

            }

            @Override
            public void onBtConnected() {
                audioSwitchUtils.switchToBluetoothHeadset();
            }

            @Override
            public void onBtDisconnected() {

            }

            @Override
            public void onBtAudioConnected() {

            }

            @Override
            public void onBtAudioDisconnected(boolean setToBt, boolean btAlive) {
                if (setToBt && btAlive) {
                    audioSwitchUtils.switchToBluetoothHeadset();
                }
            }

            @Override
            public void onHeatSetConnectLoss() {

            }

            @Override
            public void onWiredHeadSetOut() {

            }

            @Override
            public void onWiredHeadSetIn() {

            }

            @Override
            public void onAudioFocusLossTransient() {
                try {
                    if (mediaPlayer.isPlaying())
                        mediaPlayer.pause();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onAudioFocusLoss() {
                try {
                    if (mediaPlayer.isPlaying())
                        mediaPlayer.stop();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onAudioFocusGain(boolean setToBt, boolean btAlive) {
                try {
                    mediaPlayer.start();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
                if (setToBt && btAlive) {
                    audioSwitchUtils.switchToBluetoothHeadset();
                }
            }

        });
        mediaPlayer = new MediaPlayer();
    }

    private void initView() {
        mPlayMusicBtn = findViewById(R.id.btn_play_music);
        mPlayMusicBtn.setOnClickListener(this);
        mOpenBltBtn = findViewById(R.id.btn_open_blt);
        mOpenBltBtn.setOnClickListener(this);
        mCloseBltBtn = findViewById(R.id.btn_close_blt);
        mCloseBltBtn.setOnClickListener(this);
        mSwitchBltBtn = findViewById(R.id.btn_switch_blt);
        mSwitchBltBtn.setOnClickListener(this);
        mSwitchSpeakerBtn = findViewById(R.id.btn_switch_speaker);
        mSwitchSpeakerBtn.setOnClickListener(this);
        mSwitchEarphoneBtn = findViewById(R.id.btn_switch_earphone);
        mSwitchEarphoneBtn.setOnClickListener(this);
        mSwitchReceiverBtn = findViewById(R.id.btn_switch_receiver);
        mSwitchReceiverBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_play_music:
                mediaPlayer.reset();
                AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.music);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
                    mediaPlayer.prepare();
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.setLooping(true);
                mediaPlayer.start();
                break;
            case R.id.btn_open_blt:
                boolean result = audioSwitchUtils.enableBluetooth();
                Log.d(AudioSwitchUtils.TAG, result + "");
                break;
            case R.id.btn_close_blt:
                result = audioSwitchUtils.disableBluetooth();
                Log.d(AudioSwitchUtils.TAG, result + "");
                break;
            case R.id.btn_switch_blt:
                boolean success = audioSwitchUtils.switchToBluetoothHeadset();
                Log.d(AudioSwitchUtils.TAG, "切换:" + success);
                break;
            case R.id.btn_switch_speaker:
                audioSwitchUtils.switchToSpeaker();
                break;
            case R.id.btn_switch_earphone:
                audioSwitchUtils.switchToWiredHeadset();
                break;
            case R.id.btn_switch_receiver:
                audioSwitchUtils.switchToReceiver();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        mediaPlayer.stop();
        mediaPlayer.release();
        audioSwitchUtils.release(this);
        super.onDestroy();
    }
}
